package version2;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author JourWon
 * @date 2021/4/9 20:37
 */
public class inputFlie {
    /**
     * 读取文档中表格
     *
     * @param
     * @param filePath 文档路径
     * @return
     */
    public static List<List<List<String>>> IN_DATA(String filePath){
        try {
            //载入文件流
            FileInputStream in = new FileInputStream(filePath);
            List<List<List<String>>> tableList = new ArrayList<>();
            if (filePath.toLowerCase().endsWith("docx")){
                XWPFDocument docx = new XWPFDocument(in);       //得到word文档的信息
                for(XWPFTable table : docx.getTables()) {       //表格
                    for(XWPFTableRow row : table.getRows()) {   //行
                        List<List<String>> rowList = new ArrayList<>();
                        for(XWPFTableCell cell : row.getTableCells()) {
                            rowList.add(readcell(cell));
                        }
                        tableList.add(rowList);
                    }
                }
                return tableList;
            }else {
                System.out.println("请另存为.docx文件，否则出错！！！");
                return null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private final static List<String > readcell(XWPFTableCell cell){
        List<String> data = new ArrayList<>();
        List<XWPFParagraph> txts = cell.getParagraphs();
        for (XWPFParagraph txt : txts){
            data.add(txt.getText());
        }
        return data;
    }
}
